//
//  LoginView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/6.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "LoginView.h"
#import "UILabel+YBAttributeTextTapAction.h"

@interface LoginView ()<YBAttributeTapActionDelegate>

@end

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        
        UIView *alphaView = [[UIView alloc] init];
        alphaView.backgroundColor = [CSBlackColor colorWithAlphaComponent:0.7];
        [self addSubview:alphaView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenWithAnimation)];
        [alphaView addGestureRecognizer:tap];
        self.alphaView = alphaView;
        
        UIView *whiteView = [[UIView alloc] init];
        whiteView.layer.cornerRadius = 10;
        whiteView.backgroundColor = [CSWhiteColor colorWithAlphaComponent:1];
        [self addSubview:whiteView];
        self.whiteView = whiteView;
        
        
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"您还没有登录，请先登录";
        titleLabel.font = MidFont;
        titleLabel.textColor = CSBlackColor1;
        [whiteView addSubview:titleLabel];
        self.titleLable = titleLabel;
        
        /**
         分割线
         */
        UIView *sepView = [[UIView alloc]init];
        sepView.backgroundColor = LineBackGroundColor;
        [whiteView addSubview:sepView];
        self.sepView = sepView;
        
        /**
         登录按钮
         */
        
        UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [loginBtn setImage:[UIImage imageNamed:@"背景微信"] forState:UIControlStateNormal];
        loginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        loginBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        loginBtn.imageView.layer.masksToBounds = YES;
        [whiteView addSubview:loginBtn];
        self.loginBtn = loginBtn;
        
        /**
         协议
         */
        UILabel *agreementLable = [[UILabel alloc] init];
        agreementLable.text = @"登录即表示您同意“使用协议”和“隐私策略”";
        agreementLable.font = SmallFont;
        agreementLable.textColor = TextColor1;
        [whiteView addSubview:agreementLable];
        self.agreementLable = agreementLable;
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        //调整行间距
        paragraphStyle.lineSpacing = 10;
        
        [paragraphStyle setAlignment:NSTextAlignmentCenter];
        
        NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,
                                    NSFontAttributeName:SmallFont,
                                    
                                    };
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:agreementLable.text attributes:attriDict];
        
        [attributedString addAttribute: NSForegroundColorAttributeName value: TextColor1 range: [agreementLable.text rangeOfString:agreementLable.text]];
        
        [attributedString addAttribute: NSForegroundColorAttributeName value: TextColor range: [agreementLable.text rangeOfString:@"“使用协议”"]];
        
        [attributedString addAttribute: NSForegroundColorAttributeName value: TextColor range: [agreementLable.text rangeOfString:@"“隐私策略”"]];
        
        agreementLable.attributedText = attributedString;
        
        [agreementLable yb_addAttributeTapActionWithStrings:@[@"“使用协议”",@"“隐私策略”"] delegate:self];
        
        [agreementLable yb_addAttributeTapActionWithStrings:@[@"“使用协议”",@"“隐私策略”"] tapClicked:^(UILabel *label, NSString *string, NSRange range, NSInteger index) {
            
            NSLog(@"%@",string);
            
        }];
        
        [self setConstraints];
        
    }
    
    return self;
}

-(void)setConstraints
{
    
    
    
    /**
     背景色
     */
    [self.alphaView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     内容view
     */
    [self.whiteView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(Iphone5?-30:-45);
        make.left.mas_equalTo(self).offset(Iphone5?30:45);
    }];
    
    
    /**
     标题
     */
    
    [self.titleLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.whiteView).offset(24);
        make.centerX.mas_equalTo(self.whiteView);
        make.left.mas_greaterThanOrEqualTo(self.whiteView).offset(10);
        make.right.mas_lessThanOrEqualTo(self.whiteView).offset(-10);
    }];
    
    [self.sepView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLable.mas_bottom).offset(25);
        make.centerX.mas_equalTo(self.whiteView);
        make.left.mas_equalTo(self.whiteView).offset(50);
        make.right.mas_equalTo(self.whiteView).offset(-50);
        make.height.mas_equalTo(1);
    }];
    
    [self.loginBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.whiteView);
        make.top.mas_equalTo(self.sepView.mas_bottom).offset(20);
        make.height.width.mas_equalTo(60);
    }];
    
    
    [self.agreementLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(27);
        make.centerX.mas_equalTo(self.whiteView);
        make.left.mas_greaterThanOrEqualTo(self.whiteView).offset(5);
        make.right.mas_lessThanOrEqualTo(self.whiteView).offset(-5);
        make.bottom.mas_equalTo(self.whiteView).offset(-16);
    }];
    
    
    
    
}





- (void)showWithAnimation {
    
    
    
    [self addViews];
    
    CGRect frame = self.whiteView.frame;
    self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    self.whiteView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        self.whiteView.y = frame.origin.y;
    }];
    
}


- (void)hiddenWithAnimation {
    CGFloat height = self.whiteView.frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.whiteView.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT + height / 2);
        self.alphaView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    
    [self removeFromSuperview];
}




@end
